layui.use(['form','layer','upload','laydate','treeSelect','selectN'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var upload = layui.upload,
     selectN=layui.selectN;

    var date=new Date();var year = date.getFullYear();
    let years = [];
    for(let i=year;i>=2000;i--){
        years.push({"id":`${i}`,"name":`${i}`+"级"});
    }
    var catData = [{"id":1,"name":"教师"},{"id":2,"name":"学生","children":years}];
    //无限级分类-所有配置
    var catIns = null;

    catIns=selectN({
        //元素容器【必填】
        elem: '#realname'
        //候选数据【必填】
        ,data: catData
        //数据的键名
        // ,field:{idName:'id',realName:'name',age:'children'}
        //空值项提示，可设置为数组['请选择省','请选择市','请选择县']
        ,tips: ['请选择','请选择年级']

    });

    var tempUserType="0";
    //监听仪器是否可用开关
    form.on('switch(switchTest)', function(data){
        if (this.checked) {
            $("#userType").val('0');
            tempUserType = "0";
        } else {
            $("#userType").val('1');
            tempUserType = "1";
        }
    });



    var treeSelect= layui.treeSelect;//树形下拉框
    treeSelect.render({
        elem: '#tree-select',
        data: '/role/getAll',
        type: 'get',
        placeholder: '权限角色',// 占位符
        search: true,// 是否开启搜索功能：true/false，默认false
        click: function(data){
            $("#tree-select").val(data.current.id);
        },
        success: function (data) { // 加载完成后的回调函数
            // $("#tree-select").val("0");
        }
    });

    //监听是否启用角色开关
    form.on('switch(switchTest)', function(data){
        if (this.checked) {
            $("#userType").val("0")
        } else {
            $("#userType").val("1")
        }
    });



    form.on("submit(addUser)",function(data){

        $.ajax({
            url: "/verifyUserName",
            type: "POST",
            data: {loginName:data.field.loginName},
            success: function (d) {
                if (d.code != 200) {
                    var ids=data.field.realname.split(",");
                    if(ids.length!=0) {
                        data.field.realname = ids[0];
                        if (ids.length == 2)
                            data.field.age = ids[1];
                        else
                            data.field.age = "";
                    }
                    else
                        data.field.realname="";

                    data.field.userType= tempUserType;
                    //弹出loading
                    var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});

                    $.ajax({
                        url: '/admin/user/add',
                        data: data.field,
                        type: 'post',
                        success: function (data) {
                            if(data.code == 200) {
                                top.layer.close(index);
                                top.layer.msg("添加成功！");
                                layer.closeAll("iframe");
                                //刷新父页面
                                parent.location.reload();
                            } else {
                                top.layer.close(index);
                                top.layer.msg("添加失败！");
                            }
                        },
                        error:function () {
                            top.layer.close(index);
                            top.layer.msg("添加失败！");
                        }
                    });

                } else {
                    layer.msg('添加失败！该用户名已存在！');

                }
            }
        })




        return false;
    })

    //格式化时间
    function filterTime(val){
        if(val < 10){
            return "0" + val;
        }else{
            return val;
        }
    }
    //定时发布
    var time = new Date();
    var submitTime = time.getFullYear()+'-'+filterTime(time.getMonth()+1)+'-'+filterTime(time.getDate())+' '+filterTime(time.getHours())+':'+filterTime(time.getMinutes())+':'+filterTime(time.getSeconds());

})